﻿namespace EFS.RC.CCC.Deployment.Tables
{
	public static class EndPoints
	{
		/// Returns an object from a data row
		public static EFS.RC.CCC.Deployment.Entities.EndPoint FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CCC.Deployment.Entities.EndPoint dataObj = new EFS.RC.CCC.Deployment.Entities.EndPoint();
			
			// ID
			dataObj.ID = EFS.DB.Common.Functions.Value.Int32(row, "ID");

			// SIP
			dataObj.URI = EFS.DB.Common.Functions.Value.String(row, "URI");

			// MODEL
			//dataObj.Model = (EFS.RC.CCC.Deployment.Entities.EndPoint.Models)EFS.DB.Common.Functions.Value.Int32(row, "MODEL");

			// OFFICE_ENDPOINT_ID
			//dataObj.OfficeEndPointID = EFS.DB.Common.Functions.Value.Int32(row, "OFFICE_ENDPOINT_ID");

			// ok
			return dataObj;
		}

		/// GetAll
		public static EFS.RC.CCC.Deployment.Entities.EndPoint[] GetAll()
		{
			// get data
			System.Data.DataSet resultDataSet = EFS.RC.CCC.Deployment.Connection.Instance.DoSelect("[dbo].[EndPoints_GetAll]", null);

			// create list
			System.Collections.Generic.List<EFS.RC.CCC.Deployment.Entities.EndPoint> list = new System.Collections.Generic.List<EFS.RC.CCC.Deployment.Entities.EndPoint>();

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					list.Add(FromTableRow(row));
				}
			}

			// ok
			return list.ToArray();
		}

		/// GetByID
		public static EFS.RC.CCC.Deployment.Entities.EndPoint GetByID(System.Int32 id)
		{
			// get data
			System.Data.DataSet resultDataSet =
				EFS.RC.CCC.Deployment.Connection.Instance.DoSelect(
					"[dbo].[EndPoints_GetByID]",
					new System.Data.SqlClient.SqlParameter[] 
					{
						new System.Data.SqlClient.SqlParameter("@id", id)
					});

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					return FromTableRow(row);
				}
			}

			// none found
			return null;
		}

		/// GetBySIP
		public static EFS.RC.CCC.Deployment.Entities.EndPoint GetBySIP(System.String sip)
		{
			// get data
			System.Data.DataSet resultDataSet =
				EFS.RC.CCC.Deployment.Connection.Instance.DoSelect(
					"[dbo].[EndPoints_GetBySip]",
					new System.Data.SqlClient.SqlParameter[] 
					{
						new System.Data.SqlClient.SqlParameter("@sip", sip)
					});

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					return FromTableRow(row);
				}
			}

			// none found
			return null;
		}

		/// GetByModel
		/*public static EFS.RC.CCC.Deployment.Entities.EndPoint[] GetByModel(
			EFS.RC.CCC.Deployment.Entities.EndPoint.Models model)
		{
			// get data
			System.Data.DataSet resultDataSet =
				EFS.RC.CCC.Deployment.Connection.Instance.DoSelect(
					"[dbo].[EndPoints_GetByModel]",
					new System.Data.SqlClient.SqlParameter[] 
					{
						new System.Data.SqlClient.SqlParameter("@model", (System.Int32)model)
					});

			// create list
			System.Collections.Generic.List<EFS.RC.CCC.Deployment.Entities.EndPoint> list = new System.Collections.Generic.List<EFS.RC.CCC.Deployment.Entities.EndPoint>();

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					list.Add(FromTableRow(row));
				}
			}

			// ok
			return list.ToArray();
		}*/

		/// Create
		/*public static System.Int32? Create(
			System.String sip,
			EFS.RC.CCC.Deployment.Entities.EndPoint.Models model,
			System.Int32? officeEndPointID,
			System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			System.Data.SqlClient.SqlParameter sqlParamOEID = new System.Data.SqlClient.SqlParameter("@office_endpoint_id", System.DBNull.Value);
			if (officeEndPointID.HasValue)
			{
				sqlParamOEID = new System.Data.SqlClient.SqlParameter("@office_endpoint_id", officeEndPointID.Value);
			}

			// ok
			return
				EFS.RC.CCC.Deployment.Connection.Instance.Create(
					"[dbo].[EndPoints_Create]",
					new System.Data.SqlClient.SqlParameter[]
				{
					new System.Data.SqlClient.SqlParameter("@sip", sip),
					new System.Data.SqlClient.SqlParameter("@model", (System.Int32)model),
					sqlParamOEID
				},
				sqlTransaction);
		}

		/// Update
		public static void Update(
			System.Int32 id,
			System.String sip,
			EFS.RC.CCC.Deployment.Entities.EndPoint.Models model,
			System.Int32? officeEndPointID)
		{
			System.Data.SqlClient.SqlParameter sqlParamOEID = new System.Data.SqlClient.SqlParameter("@office_endpoint_id", System.DBNull.Value);
			if (officeEndPointID.HasValue)
			{
				sqlParamOEID = new System.Data.SqlClient.SqlParameter("@office_endpoint_id", officeEndPointID.Value);
			}

			EFS.RC.CCC.Deployment.Connection.Instance.Update(
				"[dbo].[EndPoints_Update]",
				new System.Data.SqlClient.SqlParameter[]
				{
					new System.Data.SqlClient.SqlParameter("@id", id),
					new System.Data.SqlClient.SqlParameter("@sip", sip),
					new System.Data.SqlClient.SqlParameter("@model", (System.Int32)model),
					sqlParamOEID
				});
		}*/

		/// Delete
		public static void Delete(System.Int32 id)
		{
			EFS.RC.CCC.Deployment.Connection.Instance.Delete("[dbo].[EndPoints_Delete]", id);
		}
	}

}